home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COLLIDE.ZIP / COLLIDE.DOC next >
Encoding:
Text File  |  1995-04-18  |  4.3 KB  |  98 lines

  1.  
  2.         A Pixel-Precision Method For Detecting Sprite Collision
  3.                                 v 1.0
  4.                            by Mark Mackey
  5.  
  6. ---
  7. Introduction
  8. ----
  9.  
  10. A common query on the USENET newsgroup rec.games.programmer over the 
  11. years has been how to do fast pixel-precision collision detection. A 
  12. number of people have presented some excellent algorithms for extending 
  13. and speeding up bounding-box collision detection between large numbers of 
  14. objects (for instance, dividing the play area into subfields and sorting 
  15. the sprites into these subfields, thus greatly reducing the number of 
  16. sprite-sprite collisions that have to be checked in the first place). 
  17.  
  18. However, there have been little information available on how to detect 
  19. collisions efficiently at the pixel level. I thus present here some code 
  20. from my game XQuest (blatant plug) which checks for collisions at the 
  21. pixel level. The routine assumes that the bounding boxes for the two 
  22. sprites overlap, and hence should be relatively easy to add to an 
  23. existing bounding-box collision detection routine. This code is fast, 
  24. relatively efficient in terms of space (requiring 4 bytes per row of each 
  25. sprite), and even works! The one limitation is that sprites are assumed 
  26. to be 32 pixels or less in width. For larger sprites, you could either 
  27. treat them as 2 or more 32-pixel wide objects, or with a bit of creative 
  28. thought this routine could be rewritten to allow for 64-pixel wide 
  29. objects on the 386 (left as an exercise for the reader :). 
  30.  
  31. The code in the enclosed file COLLIDE.PAS is organised as a Turbo Pascal 
  32. unit, but all of the essential code is in assembly language and could be 
  33. easily ported to work under C or in a pure asm program. If any of the 
  34. Pascalisations confuse you just let me know and I'll explain :). Also, if 
  35. you need a version of this code to work on a 286 then let me know and 
  36. I'll send it to you (but be warned: it's not nearly as nice :).
  37.  
  38. ---
  39. The Algorithm 
  40. ---
  41.  
  42. The first step is to create a 'transparency' mask for each sprite. The 
  43. mask consists of a dword for each row of the sprite, with each bit being 
  44. a 0 if the corresponding pixel is 'empty', and a 1 otherwise. For 
  45. example, if a row of your sprite looked like (colour indexes, 0 being 
  46. transparent) 
  47.  
  48. 0  0  0  1  23  42  0  1  56  0  0  0  0  0 
  49.  
  50. the the corresponging mask bytes would be 
  51.  
  52. 00011101 10000000 00000000 00000000 =  1D 80 00 00 
  53.  
  54. The MakeMask procedure given will produce such a mask from a sprite 
  55. supplied in the XLib linear bitmap format (with pixels of colour zero 
  56. being transparent), and is easily adaptable to your own sprite format. 
  57.  
  58. OK, now the hard part. We have found in our general-purpose bounding-box 
  59. collision detection routine that two sprites' bounding boxes have 
  60. collided (ie their masks overlap). 
  61.  
  62. Let the leftmost sprite have (x1,y1) as the coords of its top left 
  63. corner, and the rightmost one (x2,y2). Take the mask entry for row 
  64. |y2-y1| of the topmost sprite and shift it left by the difference in the 
  65. x-coordinates (x2-x1). AND this value with the 1st mask entry of the 
  66. lower sprite. If the result is non-zero then a collision occurred on this 
  67. row. If not, then shift row |y2-y1+1| and compare it to row 2 of the 
  68. lower sprite, and so on, until you reach the bottom of one of the 
  69. sprites. If no collision has been detected by this time, then the sprites 
  70. didn't collide. Simple, eh? 
  71.  
  72. This routine is quite fast, requiring only a MOV, a SHL and an AND for
  73. each row checked, and only checking those rows that overlap.
  74.  
  75. ---
  76. The Legal Bit
  77. ---
  78.  
  79. This software is (C) Copyright 1995 Mark Mackey. Permission is given to 
  80. distribute this code freely, or to distribute modified forms of this 
  81. software provided that the author is acknowledged and this copyright 
  82. notice retained. Permission is also given to utilise this code in 
  83. original or modified form in any software provided that the author is 
  84. acknowledged. 
  85.  
  86. I can be contacted by
  87.  
  88. Email: mdm1004@cus.cam.ac.uk 
  89. WWW: http://www.ch.cam.ac.uk/MMRG/mdm.html
  90. Snail: c/o Trinity Hall,
  91.            Cambridge CB2 1TJ
  92.            UK
  93.  
  94. These addresses will be in use until at least October 1996. Please let me 
  95. know if you found this code helpful/useful/rubbish/whatever or if you 
  96. improve it :) 
  97.  
  98.